import { request } from '../request';

/**
 * @desc 每几日一学计划数据
 */
export type CourseType = {
  courseGuid: string;
  cycleTime: number;
  name: string;
  openStatus: number;
  title: string;
};

/**
 * @desc 计划学习数据
 */
export type Chartdata = {
  endDate: string;
  noStudyPersonNum: number;
  startDate: string;
  studyPersonNum: number;
};

export type CourseDataDTO = {
  pageNum: number;
  pageSize: number;
  /** 当前企业ID */
  curEntId: string;
  /** 选择的企业ID */
  entId: string;
};

export type CourseDataVO = {
  courseGuid: string;
  cycleTime: number;
  name: string;
  openStatus: number;
  title: string;
};

async function postPlanDataList(params: CourseDataDTO) {
  const { data } = await request<CourseDataVO[]>({
    url: '/safe/api/statistics/getEntCourseList',
    params,
    noAutoShowErrorMessage: true,
  });
  return data;
}

/**
 * @desc 计划列表(协会/企业自己的) 没有已关闭的计划
 */
async function getCourseList(curEntId: string) {
  const { data } = await request<CourseType[]>({
    url: '/safe/api/statistics/getCourseList',
    params: {
      pageNum: 1,
      pageSize: 9999,
      curEntId,
    },
    noAutoShowErrorMessage: true,
  });
  return data;
}

/**
 * @desc 计划列表(企业其他的)
 */
async function getNowOwnCourseList(curEntId: string) {
  const { data } = await request<CourseType[]>({
    url: '/safe/api/statistics/getNoOwnCourseList',
    params: {
      curEntId,
    },
    noAutoShowErrorMessage: true,
  });
  return data;
}
/**
 * @desc 获取图表学习信息
 */
async function getStatisCycleStudyList(courseGuid: string) {
  const { data } = await request<Chartdata[]>({
    url: '/safe/api/statistics/getCycleStudyList',
    params: {
      pageNum: 1,
      pageSize: 9999,
      courseGuid,
    },
    noAutoShowErrorMessage: true,
  });
  return data;
}
export interface PlanStatisticsSearchParams {
  entId: string;
}

export interface PlanListItem {
  startTime: string;
  endTime: string;
  cycleTime: string;
  name: string;
  source: string;
  allPersonNum: number;
  studyPersonNum: number;
  noStudyPersonNum: number;
  studyRatio: string;
  type: number;
  planCourseId: string;
  cycleId: string;
  No: number;
}

/**
 * @desc 分页查询计划学习情况列表(协会/企业自己的)
 */
async function planStudySituationList({ entId }: Partial<PlanStatisticsSearchParams>, pageIndex = 1, pageSize = 10) {
  const { data } = await request<{ list: PlanListItem[]; total: number }>({
    url: '/safe/api/statistics/planStudySituationList',
    params: {
      pageNum: pageIndex,
      pageSize: pageSize,
      entId,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    //编号
    e.No = index + 1;
  });
  return data;
}
/**
 * @desc 分页查询计划学习情况列表(企业其他的)
 */
async function noOwnPlanStudySituationList(
  { entId }: Partial<PlanStatisticsSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<{ list: PlanListItem[]; total: number }>({
    url: '/safe/api/statistics/noOwnPlanStudySituationList',
    params: {
      pageNum: pageIndex,
      pageSize: pageSize,
      entId,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    //编号
    e.No = index + 1;
  });
  return data;
}

export type PlanStudySituationDataDTO = {
  entId: string;
  curEntId?: string;
  pageNum?: number;
  pageSize?: number;
};

async function postPlanStudySituationList(params: PlanStudySituationDataDTO) {
  const { data } = await request<{ list: PlanListItem[]; total: number }>({
    url: '/safe/api/statistics/entPlanStudySituationList',
    params,
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    //编号
    e.No = index + 1;
  });
  return data;
}

export interface PersonStudyListSearchParams {
  entId: string;
  personName?: string;
  phone?: string;
  finishState?: number | string;
  planCourseId: string;
  cycleId: string;
}

export interface PlanPersonListItem {
  name: string;
  finishStatus: number;
  entId: string;
  perIdentity: string;
  phone: string;
  No: number;
  extraInfo?: { title: string; value: string }[];
  joinList?: { name: string }[];
}

/**
 * @desc 人员学习情况明细列表
 */
async function getPersonStudyDetailList(
  url: string,
  postParams: Partial<PersonStudyListSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<{ list: PlanPersonListItem[]; total: number }>({
    url: url,
    params: {
      pageNum: pageIndex,
      pageSize: pageSize,
      ...postParams,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    //编号
    e.No = index + 1;
  });
  return data;
}

export {
  getCourseList,
  getNowOwnCourseList,
  getPersonStudyDetailList,
  getStatisCycleStudyList,
  noOwnPlanStudySituationList,
  planStudySituationList,
  postPlanDataList,
  postPlanStudySituationList,
};
